﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;

/// <summary>
/// Author: Ted Kolovos (MCSD.NET, PMP) ©2009 EzBiz, LLC
/// FAQ (w/ usage examples): http://www.CSharpUniversity.com/aspneterrorhandler
/// 
/// Summary: The csharpuniversity.ErrorWeb application lets you
/// easily monitor errors (exceptions) that occured in a production ASP.NET
/// website.
/// 
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
public partial class ErrorTracking : System.Web.UI.Page
{
    enum SearchType
    {
        Custom, Recent100, Recent500, Past7Days
    }
    SearchType mSearchType;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string sApplicationName = ConfigurationManager.AppSettings["ApplicationName"];
            lblApplicationName.Text = sApplicationName;

            //string sErrorWebVersion = ConfigurationManager.AppSettings["Version"];
            //lblErrorWebVersion.Text = sErrorWebVersion;
            //lblErrorTrackingVersion.Text = csharpuniversity.ErrorTracking.ErrorLog.getVersion();
        }
    }
    protected void btnSearchErrors_Click(object sender, EventArgs e)
    {
        mSearchType = SearchType.Custom;
        GridView1.DataSourceID = "ErrorTrackingSource";
    }
    protected void btnDisplayRecent100_Click(object sender, EventArgs e)
    {
        mSearchType = SearchType.Recent100;
        GridView1.DataSourceID = "ErrorTrackingSource";
    }
    protected void btnDisplayRecent500_Click(object sender, EventArgs e)
    {
        mSearchType = SearchType.Recent500;
        GridView1.DataSourceID = "ErrorTrackingSource";
    }
    protected void ErrorTrackingSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        if (!Page.IsValid)
        {
            e.Cancel = true;
            return;
        }

        switch (mSearchType)
        {
            case SearchType.Recent100:
                e.InputParameters["MaxNumOfRecords"] = 100;
                break;
            case SearchType.Recent500:
                e.InputParameters["MaxNumOfRecords"] = 500;
                break;
            case SearchType.Past7Days:
                e.InputParameters["StartDate"] = DateTime.Now.AddDays(-7);
                e.InputParameters["EndDate"] = DateTime.Now;
                e.InputParameters["MaxNumOfRecords"] = 99999;
                break;
            case SearchType.Custom:
                if (!string.IsNullOrEmpty(txtErrorId.Text))
                    e.InputParameters["ErrorId"] = int.Parse(txtErrorId.Text);

                if (!string.IsNullOrEmpty(txtStartDate.Text)
                    && !string.IsNullOrEmpty(txtEndDate.Text))
                {
                    e.InputParameters["StartDate"] = DateTime.Parse(txtStartDate.Text);
                    e.InputParameters["EndDate"] = DateTime.Parse(txtEndDate.Text).AddDays(1);
                }
                if (!string.IsNullOrEmpty(txtErrorMessage.Text))
                    e.InputParameters["ErrMessage"] = txtErrorMessage.Text;
                if (!string.IsNullOrEmpty(txtStackTrace.Text))
                    e.InputParameters["StackTrace"] = txtStackTrace.Text;
                if (!string.IsNullOrEmpty(txtLocation.Text))
                    e.InputParameters["Location"] = txtLocation.Text;
                if (!string.IsNullOrEmpty(txtClientIP.Text))
                    e.InputParameters["ClientIP"] = txtClientIP.Text;
                if (!string.IsNullOrEmpty(txtServerName.Text))
                    e.InputParameters["ServerName"] = txtServerName.Text;
                if (!string.IsNullOrEmpty(txtCustom1.Text))
                    e.InputParameters["Custom1"] = txtCustom1.Text;
                if (!string.IsNullOrEmpty(txtCustom2.Text))
                    e.InputParameters["Custom2"] = txtCustom2.Text;
                if (!string.IsNullOrEmpty(txtCustom3.Text))
                    e.InputParameters["Custom3"] = txtCustom3.Text;
                if (!string.IsNullOrEmpty(txtCustom4.Text))
                    e.InputParameters["Custom4"] = txtCustom4.Text;
                if (!string.IsNullOrEmpty(txtCustom5.Text))
                    e.InputParameters["Custom5"] = txtCustom5.Text;
                if (rbPartialMatch.Checked)
                    e.InputParameters["PartialMatch"] = true;
                if (!string.IsNullOrEmpty(txtMaxNumRecords.Text))
                    e.InputParameters["MaxNumOfRecords"] = int.Parse(txtMaxNumRecords.Text);
                if(rbOldestFirst.Checked)
                    e.InputParameters["OldestFirst"] = true;
                break;
        }
    }

    protected void btnDisplayThisWeek_Click(object sender, EventArgs e)
    {
        mSearchType = SearchType.Past7Days;
        GridView1.DataSourceID = "ErrorTrackingSource";
    }

    protected void ValidateStartEndDate(object source, ServerValidateEventArgs args)
    {
        if (!string.IsNullOrEmpty(txtStartDate.Text) &&
            string.IsNullOrEmpty(txtEndDate.Text))
        {
            DateValidator.ErrorMessage = "If start date is specified, must also specify end date";
            args.IsValid = false;
        }
        else if (!string.IsNullOrEmpty(txtEndDate.Text) &&
            string.IsNullOrEmpty(txtStartDate.Text))
        {
            DateValidator.ErrorMessage = "If end date is specified, must also specify start date";
            args.IsValid = false;
        }
        else
            args.IsValid = true;
    }

    protected void ValidateErrorId(object source, ServerValidateEventArgs args)
    {
        if (!string.IsNullOrEmpty(txtErrorId.Text))
        {
            int validInt;
            bool isValidNum = int.TryParse(txtErrorId.Text, out validInt);
            if (isValidNum)
                args.IsValid = true;
            else
                args.IsValid = false;
        }
    }
    protected void ErrorTrackingSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
    {
        DataSet returnDS = (DataSet)e.ReturnValue;
        //If returnDS is null, there was probably an Exception that happened
        //when the ObjectDataSource was called.
        if (returnDS == null)
            return;

        //Determine if any data was returned by the ErrorTracking assembly
        if (returnDS.Tables.Count > 0 &&
            returnDS.Tables[0].Rows.Count > 0)
            lblSearchResults.Text = "";
        else
            lblSearchResults.Text = "No records were found that match the above criteria.";
    }
}
